﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using He.Web.UI;
using He.Common;
using He.API.OAuth.QQHelper;

namespace He.Web.user.oauth.qq
{
    public partial class bind : System.Web.UI.Page
    {
        public Model.member Member = new Model.member();
        BLL.member mbll = new BLL.member();
        QQHelper Helper = new QQHelper();
        public string openid = "";
        public string access_token = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }

            BLL.member mgr = new BLL.member();
            string username = User.Identity.Name;
            if (mgr.Exists(username))
            {
                this.Member = mgr.GetModel(username);
            }
            else
            {
                Response.Redirect("~/error.aspx?s=用户不存在");
            }

            if (!this.Page.IsPostBack)
            {
                GetReturn();
            }
        }

        private void GetReturn()
        {
            string code = base.Request["code"];
            if (!string.IsNullOrEmpty(code))
            {
                if (base.Request["state"] == this.Session["state"].ToString())
                {
                    Dictionary<string, object> dic1 = Helper.get_access_token(code, "bind.aspx");
                    if (dic1 != null)
                    {
                        access_token = dic1["access_token"].ToString();
                    }
                    else
                    {
                        Response.Write("获取access token值失败！");
                        Response.End();
                    }
                    Dictionary<string, object> dic2 = Helper.get_open_id(access_token);
                    if (dic2 != null)
                    {
                        openid = dic2["openid"].ToString();

                        if (mbll.ExistsQQOpenid(openid))
                        {
                            lblMsg.Text = "该QQ已和您的其他会员账号绑定，请勿重复绑定。";
                            return;
                        }

                        Dictionary<string, object> dic3 = Helper.get_user_info(access_token, openid);

                        if (dic3 != null)
                        {
                            Member.qq_openid = openid;
                            Member.figure_url = dic3["figureurl_2"].ToString();
                            mbll.Update(Member);
                            Response.Redirect("bind.aspx");
                        }
                        else
                        {
                            Response.Write("获取user_info失败！");
                            Response.End();
                        }
                    }
                    else
                    {
                        Response.Write("获取openid值失败！");
                        Response.End();
                    }
                }
            }
        }

        protected void btnRelieve_Click(object sender, EventArgs e)
        {
            Member.qq_openid = string.Empty;
            new BLL.member().Update(Member);
            Response.Redirect("bind.aspx");
        }
    }
}